package str.reg;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Describe
 *
 * @author maguangming
 * @since 2022/9/2 16:31
 */
public class RegDemo {

    public static void main(String[] args) {


        //转义
        /*
        String one = "\\";
        String two = "\\\\";
        String three = "\\\\\\";
        System.out.println(one);
        System.out.println(two);
        System.out.println(three);
        System.out.println(one.matches("\\\\"));
        System.out.println(two.matches("\\\\\\\\"));
        System.out.println(three.matches("\\\\\\\\\\\\"));
        */

        //换行
//        String text = "[TagA]first line 第一行\n" +
//                "[TagA]second line 第二行";

//        matcherAndPrint("(\\[TagA\\])(.*)", text);

        String text2 = "[TagB][TagA]first line 第一行\n" +
                "[TagA]second line 第二行[/TagB]?ABC其他";

        matcherAndPrint("(\\[TagB\\])([\\s\\S]*)(\\[/TagB\\])", text2);

    }

    private static void matcherAndPrint(String reg, String text) {
        System.out.println("********************");
        System.out.println("raw text: " + text);
        System.out.println("reg: " + reg);
        Pattern pattern = Pattern.compile(reg);
        final Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            System.out.println("----------");
            System.out.println("group: " + matcher.group());
            System.out.println("pos: " + matcher.start() + ":" + matcher.end());
            System.out.println("groupCount: " + matcher.groupCount());
            for (int i = 0; i < matcher.groupCount(); i++) {
                System.out.println("group" + (i + 1) + ": " + matcher.group(i + 1));
            }

        }
    }
}
